<?php
$activityId = intval($_SESSION['SelectedActivity']);

foreach($_REQUEST['perf'] as $value)
{
    if(!empty($value))
    {
        $perf=$value;
    }
}
$NRIC = $_REQUEST['barcode'];
//$Username = $_SESSION['--'];
include 'conn.php';
$result = mysql_query("Select * from activityrecord where NRIC='$NRIC' AND Date = CURDATE() and ActivityId=$activityId");
$centreName = "";

$qry = "Select CentreName from centre where CentreId = (SELECT client.CentreId from centre, client WHERE client.NRIC = '$NRIC' AND centre.CentreId = client.CentreId)";
$result1 = mysql_query($qry);

while ($row = mysql_fetch_array($result1)) {
    $centreName = $row['CentreName'];
}

$result2 = mysql_query("SELECT * FROM attendancerecord WHERE DATE( SignInTimeStamp ) = CURDATE( ) AND NRIC = '$NRIC'");
//$result2 = mysql_query("Select * from client,centre where NRIC='$NRIC' AND centre.CentreName='" . $_SESSION['centreName'] . " AND client.CentreId=$centerId");
$userCenterName = $_SESSION["centreName"];
if ($centreName == $userCenterName) {
    if (mysql_num_rows($result2) == 0) {
        echo "<font color='red'>This elderly didn't come today!</font>";
    } else {
        if (mysql_num_rows($result) == 0) {
            $sql = "INSERT INTO activityrecord(NRIC,CentreId,Date,ActivityId,AuditRecords,Performance) 
	VALUES ((
	SELECT  NRIC
	FROM  client
	WHERE  `NRIC` =  '$NRIC'
	), (
	SELECT  CentreId
	FROM  client
	WHERE  NRIC =  '$NRIC'
	), NOW( ) , '$activityId','$Username',$perf
	)";
            $result = mysql_query($sql);
            echo"<br>";
            if ($result) {
                echo "<p><font color='red'>Activity Record Success!</font></p>";
                if ($userType !== "System Admin") {
                                        echo"<style='vertical-align:middle;text-align:center'><b>Scan NRIC: </b>
                                    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp<b>Performance: </b>
                    <form method = 'post' name='nricform' >
                    <input id='barcode'class='tb10'type='text' name='barcode' size='10' maxLength='9'onKeyUp='if(barcode.length==7)makeAttendance()'/>
                    <input id = 'ddlActivity' name = 'ddlActivity' type = 'hidden' value = '";
                                        echo isset($_POST['selectedActivity']) ? $_POST['ddlActivity'] : '';
                                        echo"'/>";
                                        echo"&nbsp;
                                    1:<input type='checkbox' name='perf[]' value='1' id='1' onClick='perf(this)'/>&nbsp2:
                                    <input type='checkbox' name='perf[]' value='2' id='2' onClick='perf(this)'/>&nbsp3:
                                    <input type='checkbox' name='perf[]' value='3' id='3' onClick='perf(this)'/>&nbsp4:
                                    <input type='checkbox' name='perf[]' value='4' id='4' onClick='perf(this)'/>&nbsp5:
                                    <input type='checkbox' name='perf[]' value='5' id='5' onClick='perf(this)'/>
                                    <input type='submit' name='btnActSubmit' id='btnActSubmit' value='Submit'/>
                                    </form></style><p></p>";


                                        echo"</p>";
                                    }
            } else {
                echo "<font color='red'>Acitivity Record NOT Successful!</font>";
            }
        } else {
            echo "<p><font color='red'>Activity record of the elderly has been taken already!</font></p>";
            if ($userType !== "System Admin") {
                                        echo"<style='vertical-align:middle;text-align:center'><b>Scan NRIC: </b>
                                    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp<b>Performance: </b>
                    <form method = 'post' name='nricform' >
                    <input id='barcode'class='tb10'type='text' name='barcode' size='10' maxLength='9'onKeyUp='if(barcode.length==7)makeAttendance()'/>
                    <input id = 'ddlActivity' name = 'ddlActivity' type = 'hidden' value = '";
                                        echo isset($_POST['selectedActivity']) ? $_POST['ddlActivity'] : '';
                                        echo"'/>";
                                        echo"&nbsp;
                                    1:<input type='checkbox' name='perf[]' value='1' id='1' onClick='perf(this)'/>&nbsp2:
                                    <input type='checkbox' name='perf[]' value='2' id='2' onClick='perf(this)'/>&nbsp3:
                                    <input type='checkbox' name='perf[]' value='3' id='3' onClick='perf(this)'/>&nbsp4:
                                    <input type='checkbox' name='perf[]' value='4' id='4' onClick='perf(this)'/>&nbsp5:
                                    <input type='checkbox' name='perf[]' value='5' id='5' onClick='perf(this)'/>
                                    <input type='submit' name='btnActSubmit' id='btnActSubmit' value='Submit'/>
                                    </form></style><p></p>";


                                        echo"</p>";
                                    }
        }
    }
} else {
    echo "<font color='red'>This elderly is not from the " . $_SESSION['centreName'] . " Branch.</font>";
}
?>